package com.beiding.jsonanalyzer.flow;

/**
 * 本地计算资源
 *
 * @author 丁常磊
 * @date 2022/5/12 10:59
 */

public class LocalComputeResource implements ComputeResource {

    private final LocalTreadTaskSlot[] taskSlots;


    private LocalComputeResource(LocalTreadTaskSlot[] taskSlots) {
        this.taskSlots = taskSlots;
    }

    public static  LocalComputeResource create(int n) {
        LocalTreadTaskSlot[] taskSlots = new LocalTreadTaskSlot[n];
        for (int i = 0; i < n; i++) {
            taskSlots[i] = new LocalTreadTaskSlot();
        }
        return new LocalComputeResource(taskSlots);
    }

    @Override
    public TaskSlot getOneSlot(int hash) {
        //一定非负
        if (hash < 0) {
            hash = -hash;
        }
        int c = hash % taskSlots.length;
        return taskSlots[c];
    }
    public int slotNum() {
        return taskSlots.length;
    }

    public void drop() {
        for (LocalTreadTaskSlot taskSlot : taskSlots) {
            taskSlot.drop();
        }
    }

}
